﻿using AutoMapper;
using B.S.BasicData.Domain.XU;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfaces;
using B.S.BasicData.Write.Api.Application.Command.Xu;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHandler.Xu
{  

   /// <summary>
   /// 修改箱子
   /// </summary>
    public class UpdateBoxHandler : IRequestHandler<UpdeteBoxCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<BoxModel> _boxbaseRepository;
        private readonly IMapper _mapper;

        public UpdateBoxHandler(IBaseRepository<BoxModel> boxbaseRepository,IMapper mapper) 
        {
           _boxbaseRepository = boxbaseRepository;
            _mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(UpdeteBoxCommand request, CancellationToken cancellationToken)
        {
             ApiResult<int> apiResult = new ApiResult<int>();
            var getbox= _mapper.Map<BoxModel>(request);
            var updatebox= _boxbaseRepository.Update(getbox);
            if (updatebox > 0)
            {
                apiResult.Code = ApiEnums.Success;
                apiResult.Msg = "修改成功";
                apiResult.Data = updatebox;
            }
            else
            {
                apiResult.Code = ApiEnums.Fail;
                apiResult.Msg = "修改失败";
                apiResult.Data = updatebox;
            }
            return Task.FromResult(apiResult);
        }
    }
}
